<template>
  <div class="container">
    <!-- 文章详情 -->
    <div class="content">
      <van-image class="img" fit="cover" :src="info.picUrl" />
      <div class="body">
        <p class="title">{{ info.title }}</p>
        <div class="info">
          <p class="desc">{{ info.desc }}</p>
          <p class="created_time">{{ info.createdAt }}</p>
        </div>
        <div class="detail">
          {{ info.detail }}
        </div>
      </div>
    </div>
    <!-- 评论 -->
    <div class="comment" v-if="info.comments">
      <p>评论:</p>
      <div class="content_item" v-for="it in info.comments">
        <div class="user_pic">
          <img :src="it.user.avatar" :alt="it.user.name" />
        </div>
        <div class="item_left">
          <div class="user_name">
            {{ it.user.name }}&emsp;<span class="createdTime">{{
              it.created_at
            }}</span>
          </div>
          <div class="user_content">{{ it.content }}</div>
        </div>
      </div>
    </div>
    <!-- 底部操作栏 -->
    <div class="comment">
      <div class="action_box">
        <div class="add_comment" @click="showBottom = true">
          <van-icon name="edit" size="20px" />&nbsp;评论
        </div>
        <div class="action">
          <div class="Thumbs_action">
            <van-icon
              @click="praise"
              :color="isPraise ? '#3498db' : '#767676'"
              :name="isPraise ? 'good-job' : 'good-job-o'"
              :badge="info.praise || ''"
              :badge-props="{ offset: ['10px', '15pxpx'] }"
            />
          </div>
          <div class="star_action">
            <van-icon
              @click="collect"
              :color="isCollect ? '#f39c12' : '#767676'"
              :name="isCollect ? 'star' : 'star-o'"
              :badge="info.collect || ''"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 底部弹出框弹窗 -->
    <van-popup
      class="comment_box"
      v-model:show="showBottom"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <div class="comment_header">
        <div>评论</div>
        <div class="left" @click="commentAdd">发布</div>
      </div>
      <van-field
        label-align="top"
        v-model="comment"
        rows="6"
        type="textarea"
        maxlength="200"
        placeholder="请输入评论"
        show-word-limit
      />
    </van-popup>
  </div>
</template>

<script setup>
import { useRoute } from "vue-router";
import { ref, onMounted } from "vue";
import { showToast, showSuccessToast } from "vant";
import { commentDetail } from "@/api/commend";
//评论接口
import { addOneComment } from "@/api/comment";
//点赞接口
import { praiseAdd, praiseDelete } from "@/api/praise";
//收藏接口
import { addCollect, deleteCollect } from "@/api/collect";
let route = useRoute();

let info = ref({});
//判断是否点赞的状态
let isPraise = ref(false);
//判断是否收藏的状态
let isCollect = ref(false);
//底部弹出框的显隐
let showBottom = ref(false);
//评论的双向绑定值
let comment = ref("");

//点赞
let praise = async () => {
  if (isPraise.value) {
    //取消点赞
    await praiseDelete({ type: 2, object_id: route.query.articleId });
    showSuccessToast("取消点赞成功");
  } else {
    //点赞
    await praiseAdd({ type: 2, object_id: route.query.articleId });
    showSuccessToast("点赞成功");
  }
  //刷新详情页面
  getComment();
};
//收藏
let collect=async()=>{
    if(isCollect.value){
        //取消收藏
        await deleteCollect({type:2,object_id: route.query.articleId});
        showSuccessToast("取消收藏成功");
    }else{
        //收藏
        await addCollect({type:2,object_id: route.query.articleId});
        showSuccessToast("收藏成功");
    }
    //刷新详情页面
    getComment()
}

//新增评论
let commentAdd = async () => {
  //有评论才发请求
  if (comment.value) {
    await addOneComment({
      type: 2,
      object_id: route.query.articleId,
      content: comment.value,
    });
    //评论完成 提示 刷新评论数据
    showSuccessToast("评论成功");
    getComment();
    showBottom.value = false;
  } else {
    showToast("请输入评论内容");
  }
};
//刷新文章详情的方法
let getComment = async () => {
  //获取文章详情
  let res = await commentDetail(route.query.articleId);
  info.value = res;
  isPraise.value = res.is_praise || false;
  isCollect.value = res.is_collect || false;
};

onMounted(async () => {
  getComment();
});
</script>

<style scoped lang="less">
.container {
  background-color: #fff;

  .content {
    .body {
      padding: 0 12px;
      .title {
        margin-top: 10px;
        font-size: 18px;
        font-weight: bold;
        text-align: center;
      }
      .info {
        margin-top: 10px;
        display: flex;
        color: gray;
        justify-content: space-around;
        justify-items: center;
        line-height: 100%;
        .desc {
          font-size: 14px;
        }
        .created_time {
          font-size: 14px;
        }
      }
      .detail {
        margin-top: 10px;
        text-indent: 2em;
        font-size: 16px;
        word-wrap: break-word;
        word-break: normal;
        white-space: normal;
      }
    }
  }
  .comment {
    border-top: 1px solid #ccc;
    margin-top: 10px;
    background: #f6f6f6;
    padding: 6px;
    padding-bottom: 50px;
    p {
      font-size: 16px;
    }
    .content_item {
      background: #fff;
      padding: 6px;
      display: flex;
      border-radius: 12px;
      margin-top: 12px;
      .user_pic {
        width: 42px;
        height: 42px;
        position: relative;
        img {
          width: 42px;
          height: 42px;
          object-fit: cover;
          border-radius: 50%;
        }
      }
      .item_left {
        margin-left: 10px;
        font-size: 14px;
        width: 100%;
        .user_name {
          margin-bottom: 6px;
          .createdTime {
            color: #86909c;
          }
        }
        .user_content {
          word-wrap: break-word;
          word-break: break-all;
        }
      }
    }
  }
  .action_box {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 42px;
    background: #fff;
    border-top: 1px solid rgb(232, 232, 232);
    box-shadow: 0px -2px 10px rgb(232, 232, 232);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .add_comment {
      width: 100%;
      padding-left: 16px;
      height: 100%;
      display: flex;
      justify-content: left;
      align-items: center;
      font-size: 14px;
      color: #767676;
      &:active {
        background: #e8e8e8;
      }
    }
    .action {
      height: 100%;
      width: 100%;
      --van-badge-color: black;
      display: flex;
      // justify-content: space-between;
      align-items: center;
      font-size: 20px;
      color: #767676;
      div {
        display: flex;
        align-items: center;
        justify-content: left;
        padding: 6px 6px 6px 26px;
        height: 100%;
        width: 100%;
        :deep(.van-badge) {
          color: #767676;
          background-color: rgba(0, 0, 0, 0);
          border: none;
          font-size: 8px;
          font-weight: normal;
        }
        &:active {
          background: #e8e8e8;
          height: 32px;
        }
      }
    }
  }
  .comment_box {
    width: 100%;
    height: 300px;
    .comment_header {
      padding: 10px 16px 0 16px;
      font-size: 16px;
      display: flex;
      justify-content: space-between;
      .left {
        color: #86909c;
      }
    }
  }
}
</style>